from captcha.fields import CaptchaField
from django import forms
from django.core.exceptions import ValidationError


class RegisterForm(forms.Form):
    # 变量名要与html中的一样
    username=forms.CharField(min_length=3,required=True,error_messages={
        'requested':'用户名必须输入',
        'min_length':'用户名至少三位'
    })

    password=forms.CharField(min_length=3,required=True,error_messages={
        'requested':'密码必须输入',
        'min_length':'密码最少三位'
    })

    regtime=forms.DateTimeField(required=False,error_messages={
        'invalid':'时间格式错误'
    })
    sex=forms.CharField(required=False)
    #单个字段验证
    def clean_password(self):
        password=self.cleaned_data.get('password')
        new_password=self.cleaned_data.get('new_password')
        if password and password.isdigit():
            raise ValidationError("密码不能为纯数字")
        return password



class LoginForm(forms.Form):
    captcha = CaptchaField(required=True,error_messages={'invalid':'验证码错误'})  # 验证码字段

class ManagerForm(forms.Form):
    manager_name = forms.CharField(min_length=3, required=True, error_messages={
        'requested': '用户名必须输入',
        'min_length': '用户名至少三位'

    })
    password = forms.CharField(min_length=3, required=True, error_messages={
        'requested': '密码必须输入',
        'min_length': '密码最少三位'
    })

class Change_psd(forms.Form):
    new_password = forms.CharField(min_length=3, required=True, error_messages={
        'requested': '密码必须输入',
        'min_length': '密码最少三位'
    })
    def clean_new_password(self):
        new_password = self.cleaned_data.get('new_password')
        if new_password and new_password.isdigit():
            raise ValidationError("密码不能为纯数字")
        return new_password
